<template>
    <van-button
      @click="onFollowed"
      class="follow-btn"
      round
      v-if="followed"
      size="small"
      :loading="btnLoading"
      loading-type="spinner"
      >已关注</van-button
    >
    <van-button
      class="follow-btn"
      @click="onFollowed"
      type="info"
      color="#3296fa"
      round
      size="small"
      icon="plus"
      :loading="btnLoading"
      loading-type="spinner"
      v-else
      >关注</van-button
    >
</template>

<script>
import { deleteUser, followUser } from '@/api/article.js'
export default {
  props: {
    followed: {
      type: Boolean
    },
    autId: {
      required: true
    }
  },

  data () {
    return {
      btnLoading: false
    }
  },

  // 用来v-model的自定义解析
  model: {
    prop: 'followed',
    event: 'updateFollowed'
  },

  created () {},

  methods: {
    async onFollowed () {
      this.btnLoading = true
      try {
        if (this.followed) {
          // 已关注 取消关注
          await deleteUser(this.autId)
        } else {
          // 未关注 关注
          await followUser(this.autId)
        }
        this.$emit('updateFollowed', !this.followed)
        this.btnLoading = false
      } catch (err) {
        console.dir(err)
      }
    }
  }
}
</script>

<style scoped lang="less"></style>
